package vendas.web.action;

import gminet.infra.business.BusinessException;
import gminet.infra.db.TransactionException;
import gminet.infra.web.WebException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import vendas.business.LoginBusiness;
import vendas.business.UserBusiness;
import vendas.dto.User;

@ManagedBean(name = "userAction")
@RequestScoped
public class UserAction extends WebAction {

    private static final long serialVersionUID = 7615732452537044688L;

    private User user;
    private String newPassword;
    private String confirmPassword;

    public UserAction() {
        super();
    }

    public String initialize() {

        if (getLocalUser() == null) {
            return "loginUser";
        } else {
            return "homeUser";
        }
    }

    public String prepareCreateUser() throws WebException {

        return "createUser";
    }

    public String createUser() throws WebException {

        String pageReturn = "homeUser";
        try {
            if (!user.getPassword().equals(getConfirmPassword())) {
                throw new BusinessException("Confirmação de senha inválida.");
            }
            User locaUser = UserBusiness.insert(user);
            setLocalUser(locaUser);
        } catch (BusinessException be) {
            addBusinessException("frmUser", be);
            pageReturn = "createUser";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;

    }

    public String prepareEditUser() throws WebException {

        verifyLogin();
        setUser(getLocalUser());
        return "editUser";
    }

    public String editUser() throws WebException {

        verifyLogin();
        String pageReturn = "homeUser";
        try {
            UserBusiness.update(user);
            setLocalUser(user);
        } catch (BusinessException be) {
            addBusinessException("frmUser", be);
            pageReturn = "editUser";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public String prepareEditUserPassword() throws WebException {

        verifyLogin();
        setUser(getLocalUser());
        return "editUserPassword";
    }

    public String editUserPassword() throws WebException {

        verifyLogin();
        String pageReturn = "homeUser";
        try {
            if (!getNewPassword().equals(getConfirmPassword())) {
                throw new BusinessException("Confirmação de senha inválida.");
            }
            if (LoginBusiness.verifyLogin(user)) {
                user.setPassword(getNewPassword());
                UserBusiness.updatePassword(user);
            }
        } catch (BusinessException be) {
            addBusinessException("frmUserPassword", be);
            pageReturn = "editUserPassword";
        } catch (TransactionException te) {
            throw new WebException(te);
        }
        return pageReturn;
    }

    public User getUser() {
        if (this.user == null) {
            this.user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

}
